#!@BASH@
set -x
REPO_PATH="$(realpath $(dirname $0)/../)"

systemctl is-active --quiet pcsd
PCSD_INITIALLY_RUNNING=$?

if [ $PCSD_INITIALLY_RUNNING -eq 0 ]; then
        echo "* stopping pcsd service..."
        systemctl stop pcsd
fi

# export PCSD_SSL_OPTIONS=${PCSD_SSL_OPTIONS:='OP_NO_SSLv2,OP_NO_SSLv3,OP_NO_TLSv1,OP_NO_TLSv1_1'}
# export PCSD_SSL_CIPHERS=${PCSD_SSL_CIPHERS:='DEFAULT:!RC4:!3DES:@STRENGTH'}
# export PCSD_BIND_ADDR=${PCSD_BIND_ADDR:='::'}
# export PCSD_PORT=${PCSD_PORT:=2224}
# export PCSD_SESSION_LIFETIME=$PCSD_SESSION_LIFETIME:=36a00}
# export PCSD_WORKER_COUNT=1
# export PCSD_WORKER_RESET_LIMIT=2
export PCSD_DISABLE_GUI=${PCSD_DISABLE_GUI:=false}
export PCSD_DEBUG=${PCSD_DEBUG:=true}
export PCSD_DEV=${PCSD_DEV:=true}
export GEM_HOME="$REPO_PATH/@PCSD_BUNDLED_DIR_ROOT_LOCAL@"
# export GEM_PATH="$GEM_HOME:"`gem env gempath`

# RACK_ENV=production ruby -I${REPO_PATH}/pcsd ${REPO_PATH}/pcsd/pcsd.service-runner &
RACK_ENV=production ruby -I${REPO_PATH}/pcsd ${REPO_PATH}/pcsd/pcsd &
pid_ruby=$!
echo "* ruby daemon started: $pid_ruby"
sleep 5
${REPO_PATH}/pcs/pcs -d &
pid_python=$!
echo "* python daemon started: $pid_python"
function got_signal {
        echo "* killing $pid_python $pid_ruby"
        kill $pid_python
        kill $pid_ruby
        sleep 5
}

trap 'got_signal' SIGINT

echo "* ready to provide services, ctrl+c to exit..."
wait

if [ $PCSD_INITIALLY_RUNNING -eq 0 ]; then
        echo "* starting pcsd service..."
        systemctl start pcsd
fi
